Skip to content

Make clearing and restoring the may_leave flag more amenable to opt…#13713

Merged
cfallin merged 1 commit into
bytecodealliance:mainfrom
fitzgen:adapter-may-leave-reset-use-locals
Jun 23, 2026
Merged

Make clearing and restoring the may_leave flag more amenable to opt…#13713
cfallin merged 1 commit into
bytecodealliance:mainfrom
fitzgen:adapter-may-leave-reset-use-locals

Conversation

@fitzgen

@fitzgen fitzgen commented Jun 22, 2026

Copy link
Copy Markdown
Member

…imization

The clear-and-restore is now structured so that if translation doesn't actually call any functions (e.g. due to inlining) then a future dead-store elimination pass in Cranelift can remove all the flag juggling entirely (other than trapping when !may_leave):

may_leave = load vmctx+MAY_LEAVE_OFFSET      ;; (0)
trapz may_leave

...

zero = iconst 0
store zero, vmctx+MAY_LEAVE_OFFSET           ;; (1)

...

store may_leave, vmctx+MAY_LEAVE_OFFSET      ;; (2)

First, the dead-store elimination pass will see that the the store at (1): is dead and remove it. Then, the idempotent-store eliminator will recognize that the store at (2) is storing the same value that the memory location already contains and it will also be removed. The more we can reuse locals to make this idempotency obvious, rather than force Cranelift's optimizer to rediscover this information, the better.

…imization

The clear-and-restore is now structured so that if translation doesn't actually
call any functions (e.g. due to inlining) then a future dead-store elimination
pass in Cranelift can remove all the flag juggling entirely (other than trapping
when `!may_leave`):

    may_leave = load vmctx+MAY_LEAVE_OFFSET      ;; (0)
    trapz may_leave

    ...

    zero = iconst 0
    store zero, vmctx+MAY_LEAVE_OFFSET           ;; (1)

    ...

    store may_leave, vmctx+MAY_LEAVE_OFFSET      ;; (2)

First, the dead-store elimination pass will see that the the store at
(1): is dead and remove it. Then, the idempotent-store eliminator will recognize
that the store at (2) is storing the same value that the memory location already
contains and it will also be removed. The more we can reuse locals to make this
idempotency obvious, rather than force Cranelift's optimizer to rediscover this
information, the better.
@fitzgen fitzgen requested review from a team as code owners June 22, 2026 23:29
@fitzgen fitzgen requested review from cfallin and removed request for a team June 22, 2026 23:29
@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jun 23, 2026

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea -- the separate may-leave flag global is of course a cost but it seems like that should be tiny compared to the advantages we get being able to reason about it separately. LGTM!

@cfallin cfallin added this pull request to the merge queue Jun 23, 2026
Merged via the queue into bytecodealliance:main with commit 744ae13 Jun 23, 2026
52 checks passed
@fitzgen fitzgen deleted the adapter-may-leave-reset-use-locals branch June 23, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants